package com.egreen.pointsmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.States;
import com.egreen.common.pojo.entity.pointsmall.Cart;
import com.egreen.common.pojo.entity.pointsmall.PointsUser;
import com.egreen.common.pojo.entity.pointsmall.Product;
import com.egreen.pointsmall.exception.PointsmallException;
import com.egreen.pointsmall.service.ICartService;
import com.egreen.pointsmall.service.IProductCategoryService;
import com.egreen.pointsmall.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author yyf
 * @since 2023-04-04
 */
@Controller
@Slf4j
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private ICartService cartService;
    /**
     * 商品列表的展示
     * @param type
     * @param productCategoryId
     * @param session
     * @return
     */
    @GetMapping("list/{type}/{id}")
    public ModelAndView list(
            @PathVariable("type") Integer type,
            @PathVariable("id") Integer productCategoryId,
            HttpSession session
    ) {
        //查找对应的商品信息进行展示
        if (type == null || productCategoryId == null) {
            log.info("商品列表参数为空");
            throw new PointsmallException(States.PARAMETER_NULL);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        modelAndView.addObject("productList", productService.findAllByTypeAndProduct(type, productCategoryId));
        //判断是否为登录用户
        PointsUser pointsUser = (PointsUser) session.getAttribute("user");
        if (pointsUser == null) {
            //表示当前未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            //登录用户
            modelAndView.addObject("cartList",cartService.getCartVoList(pointsUser.getId()));
        }
        //商品分类
        modelAndView.addObject("list", productCategoryService.buildProductCategoryMenu());
        return modelAndView;
    }

    /**
     * 购物车
     * @param keyWord
     * @param session
     * @return
     */
    @PostMapping("/search")
    public ModelAndView search(String keyWord,HttpSession session) {
        log.info("{}",keyWord);
        if (keyWord == null) {
            throw new PointsmallException(States.PARAMETER_NULL);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("productList");
        //进行模糊查询条件构造
        QueryWrapper<Product>queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",keyWord);
        modelAndView.addObject("productList",productService.list(queryWrapper));
        //判断是否为登录用户
        PointsUser pointsUser = (PointsUser) session.getAttribute("user");
        if (pointsUser == null) {
            //表示当前未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            modelAndView.addObject("cartList",cartService.getCartVoList(pointsUser.getId()));
        }
        modelAndView.addObject("list", productCategoryService.buildProductCategoryMenu());
        return modelAndView;
    }

    /**
     * 商品详情
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/detail/{id}")
    public ModelAndView detail(@PathVariable("id")Integer id,HttpSession session){
        if (id==null){
            throw new PointsmallException(States.PARAMETER_NULL);
        }
        ModelAndView modelAndView=new ModelAndView();
        //设置页面
        modelAndView.setViewName("productDetail");
        PointsUser pointsUser = (PointsUser) session.getAttribute("user");
        if (pointsUser == null) {
            //表示当前未登录
            modelAndView.addObject("cartList", new ArrayList<>());
        } else {
            modelAndView.addObject("cartList",cartService.getCartVoList(pointsUser.getId()));
        }
        modelAndView.addObject("list", productCategoryService.buildProductCategoryMenu());
        //商品详情
        modelAndView.addObject("product",productService.getById(id));
        return modelAndView;
    }

}

